package vo
{
	import cultu.core.message.Address;
	import cultu.customer.message.Customer;
	import cultu.customer.message.Preference;
	
	import mx.collections.ArrayCollection;
	
	import vo.community.CommunityVO;

	public class CustomerVO
	{
		
		/** customer source*/
		public var srcCustomer:Customer;
		
		public function CustomerVO(o:Customer)
		{
			if(o)
			{
				srcCustomer = o;
			}
			else
			{
				srcCustomer = new Customer();
			}
		}
		
		public function get preferences():ArrayCollection
		{
			if(!srcCustomer.preferences)
				srcCustomer.preferences = new ArrayCollection();
				
			return srcCustomer.preferences;
		}
		
		public function addPreference(pCom:CommunityVO,pWeight:int=0):void
		{
			var index:int = containsPreference(pCom);
			
			if(index == -1 && pCom)
			{
				var pref:Preference = new Preference();
				pref.community=pCom.srcCommunity;
				pref.score = pWeight;
				preferences.addItem(pref);
			}
		}
		
		public function removePreference(pCom:CommunityVO):void
		{
			if(!pCom)
				return;
			
			var index:int = containsPreference(pCom);
			
			if(index > -1)
				preferences.removeItemAt(index);
		}
		
		public function containsPreference(pCom:CommunityVO):int
		{
			for(var i:int=0;i<preferences.length;i++)
			{
				var pPref:Preference = preferences[i] as Preference;
				
				if(pPref != null && pCom.equals(new CommunityVO(pPref.community)))
				{
					return i;
				}
			}
			return -1;
		}
		
		public function get address():Address
		{
			if(!srcCustomer.address)
				srcCustomer.address = new Address();
			
			return srcCustomer.address;
		}
		
		public function get lastname():String
		{
			return srcCustomer.lastname;
		}
		
		public function get firstname():String
		{
			return srcCustomer.firstname;
		}
		
		public function get mailable():Boolean
		{
			return srcCustomer.mailable;
		}
		
		public function get coordsusable():Boolean
		{
			return srcCustomer.coordsUsable;
		}
		
		public function get promotionable():Boolean
		{
			return srcCustomer.promotionable;
		}
		
		public function set lastname(value:String):void
		{
			if(value != lastname)
				srcCustomer.lastname = value;
		}
		
		public function set firstname(value:String):void
		{
			if(value != firstname)
				srcCustomer.firstname = value;
		}
		
		public function set mailable(value:Boolean):void
		{
			if(value != mailable)
				srcCustomer.mailable = value;
		}
		
		public function set promotionable(value:Boolean):void
		{
			if(value != promotionable)
				srcCustomer.promotionable = value;
		}
		
		public function set coordsusable(value:Boolean):void
		{
			if(value != coordsusable)
				srcCustomer.coordsUsable = value;
		}
	}
}